package stroemung;

public abstract class Dynamics {
	public void euler(double[] x, double dt){
		double[] y = f(x);
		//Vektoroperation: addition der einzelnen Komponenten in einer Schleife
		for (int i = 0; i < x.length; i++)
			x[i] += y[i] * dt;
	}
	//y1 = f(x)
	//xx = x + dt / 2 * y1
	//y2 = f(xx)
	//xx = x + dt/2 * y2
	//y3 = f(xx)
	public void runge(double[] x, double dt){
		
		double[] y1 = f(x);
		// n = Dimension
		int n = x.length;
		double[] xx = new double[n];
		
		for (int i = 0; i < n; i++){
			xx[i] = x[i] + 0.5*y1[i] * dt;
		}
		double[] y2 = f(xx);

		for (int i = 0; i < n; i++){
			xx[i] = x[i] + 0.5*y2[i] * dt;
		}
		double[] y3 = f(xx);

		for (int i = 0; i < n; i++){
			xx[i] = x[i] + y3[i] * dt;
		}
		
		double[] y4 = f(xx);

		double[] y = new double[n];

		for (int i= 0; i < n; i++)
			y[i] = (y1[i] + 2 * y2[i] + 2 * y3[i] + y4[i]) / 6;
		
		for (int i = 0; i < x.length; i++)
		    x[i] += y[i] * dt; 
	}	
	
	public abstract double[] f(double[] x );
}
